package com.wanmait.baohan.service.Impl;


import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.mapper.PostMapper;
import com.wanmait.baohan.mapper.StaffMapper;
import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.RoleService;
import com.wanmait.baohan.service.StaffRoleService;
import com.wanmait.baohan.service.StaffService;
import com.wanmait.baohan.util.CreatJobNumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2020/12/2 21:14
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private HttpServletRequest httpServletRequest;
    @Resource
    private PostMapper postMapper;
    @Resource
    private StaffRoleService staffRoleService;
    @Resource
    private RoleService roleService;
    /**
     * 查询全部员工
     * hxn
     */
    @Override
    public List<Staff> findAll(){
        return staffMapper.selectAllName();
    }

    @Override
    public List<Staff> findAllStaff() {
        List<Staff> staffs = staffMapper.selectList(null);
        staffs.forEach(staff -> {
            staff.setRoles(roleService.findAllRolesByStaffId2(staff.getId()));
        });
        return staffs;
    }
    /**
     * 查询全部员工（分页）
     * 高文强
     */
    @Override
    public PageInfo<Staff> findAllStaff(Integer page, Integer limit,String name ,String department,String post) {
        List<Staff> staff = staffMapper.selectAllDepartment(name, department);
        for (Staff staff1 : staff) {
            List<Post> post1 = postMapper.selectPostByID(staff1.getId(), post);
            staff1.setPost(post1);
            String str = "";
            for (Department department1 : staff1.getDepartment()) {
                str = str +department1.getName()+",";
            }

            if(str.length()>0){
                str = str.substring(0, str.length()-1);
            }

            System.out.println(str);
            staff1.setDepartmentName(str);
            String string = "";
            for (Post post2 : staff1.getPost()) {
                string = string + post2.getName()+",";
            }
            if(string.length()>0){
                string = string.substring(0, string.length()-1);
            }

            staff1.setPostName(string);

        }
        ListIterator<Staff> staffListIterator = staff.listIterator();
        while (staffListIterator.hasNext())
        {
            Staff staff1 = staffListIterator.next();
            if(staff1.getPostName().indexOf(post)==-1){
                staffListIterator.remove();
            }
        }

        PageInfo<Staff> pageInfo = new PageInfo<>(staff,7);
        return pageInfo;
    }
    /**
     * 查询单个员工详情
     * 王森明
     */
    @Override
    public Staff findOneById(Integer id) {
        //详情和职位
        Staff staff = staffMapper.findOneById(id);
        //所有部门
        List<Department> departmentsById = staffMapper.findDepartmentsById(id);

        String post="";
        String dept="";
        Set<String> set =new HashSet();

        //部门不为空
        if(departmentsById.size()!=0){
            for (Department department : departmentsById) {
                set.add(department.getName());
            }
            staff.setDepartment(departmentsById);
            for (String s : set) {
                dept=dept+s+",";
            }
            staff.setDepartmentName(dept.substring(0,dept.length()-1));
        }


        if(staff.getPost().size()!=0){
            for (Post post1: staff.getPost()) {
                post=post+post1.getName()+",";
            }
            staff.setPostName(post.substring(0,post.length()-1));
        }

        return staff;
    }

    @Override
    public List<Staff> selectAllName() {
        List<Staff> staff = staffMapper.selectAllName();
        return staff;
    }
    //更新员工资料
    @Override
    public void updateOne(Staff staff) {
      staffMapper.updateByPrimaryKeySelective(staff);
    }

    @Override
    public Staff findByNameAndPassword(String name, String password) {
        return staffMapper.selectByNameAndPassword(name, password);
    }

    @Override
    public Staff findByJobNumber(String jobNumber) {
        return staffMapper.selectByJobNumber(jobNumber);
    }

    /**
     * 添加员工
     * 周勇
     */
    @Override
    public void insertPersonnel(Staff staff,List<StaffRole>roles) {
        Integer count=(Integer)httpServletRequest.getServletContext().getAttribute("travelApplicationNumbering");
       // System.out.println("刚去出来的工号序号======"+count);
        staff.setCreateTime(new Date());
        String job=this.generateJobNumber();
        //System.out.println("刚生成的工号===="+job);
        staff.setJobNumber(job);
        staffMapper.insert(staff);
        count++;
        int newUserId=staff.getId();
       // System.out.println("新增张的id========="+newUserId);
        //System.out.println("增长后的工号序号==========="+count);
        httpServletRequest.getServletContext().setAttribute("travelApplicationNumbering",count);
        List<StaffRole>roleList=new ArrayList<>();
        for (StaffRole s:roles) {
            StaffRole staffRole=new StaffRole();
            staffRole.setRoleId(s.getRoleId());
            staffRole.setStaffId(newUserId);
            roleList.add(staffRole);
        }
        staffRoleService.insertListStaffRole(roleList);
    }

    @Override
    public String generateJobNumber() {
      Integer count=(Integer) httpServletRequest.getServletContext().getAttribute("travelApplicationNumbering");
        String job=CreatJobNumberUtil.creatJobNumber(count);
        return job ;
    }

}
